package com.legrand.core.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.legrand.core.exception.ExceptionBase;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author xiah
 * @date 2018/5/2 17:10
 */
public class JwtToken {
    private static final String SECURITY = UUID.randomUUID().toString();
    private static final Long ACCESS_TOKEN_TIME = 1000 * 60 * 10L;
    private static final Long REFRESH_TOKEN_TIME = 1000 * 60 * 60 * 24 * 30 * 12L;

    /**
     * @author xiah
     * description:生产临时token
     * param:
     * return:
     * date: 2018/5/2
     */
    public static String createToken() throws Exception {
        return create(ACCESS_TOKEN_TIME);
    }

    /**
     * @author xiah
     * description: 生产刷新token,有效时间一个月
     * param:
     * return:
     * date: 2018/5/2
     */
    public static String createPermanentToken() throws Exception {
        return create(REFRESH_TOKEN_TIME);
    }

    public static String create(Long time) throws Exception {
        Map map = new HashMap(16);
        Date date = new Date();
        map.put("typ", "JWT");
        map.put("alg", "HS256");
        String token = JWT.create().withHeader(map).withClaim("company", "legrand").withIssuer("xiah").withIssuedAt
                (date)
                .withExpiresAt(new Date(date.getTime() + time))
                .sign(Algorithm.HMAC256(SECURITY));
        return token;
        //return  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ4aWFoIiwiY29tcGFueSI6ImxlZ3JhbmQiLCJleHAiOjE1MzA3Nzg4MTYsImlhdCI6MTUzMDc3ODcxNn0.wzlIoMkZAY9MTIR62ibVpXZs-NVNONM8G3hUpcngoXg";
    }

    /**
     * @author xiah
     * description: 创建token
     * return: Map
     * date: 2018/5/3
     */
    public static Map creatTokenMap() throws Exception {
        Map map = new HashMap(16);
        map.put("token", createToken());
        map.put("refresh_token", createPermanentToken());
        return map;
    }

    /**
     * @author xiah
     * description: 验证token的正确性
     * param:
     * return:
     * date: 2018/5/2
     */
    public static Boolean checkToken(String token) throws Exception {
        JWTVerifier verification = JWT.require(Algorithm.HMAC256(SECURITY)).build();
        try {
            verification.verify(token);
            return true;
        } catch (Exception e) {
            e.hashCode();
            throw new ExceptionBase(503, "token is wrong");
        }
    }


    public static Boolean checkToken2(String token) throws Exception {
        JWTVerifier verification = JWT.require(Algorithm.HMAC256(SECURITY)).build();
        try {
            DecodedJWT ss = verification.verify(token);
            Map<String ,Claim> map = ss.getClaims();
            Claim ww =  map.get("company");
            System.out.println(ww);
            return true;
        } catch (Exception e) {
            e.hashCode();
            throw new ExceptionBase(503, "token is wrong");
        }
    }


    /*public static void main(String[] args) throws  Exception{
       String ss= create(100000L);
        checkToken2(ss);
    }*/

}
